package thread;

import java.util.Scanner;
// 内存可见性
public class ThreadDemo23 {
    public volatile static int flag =0;

    public static void main(String[] args) {
        Thread t1= new Thread(()->{
           while (flag==0){
//               try {
//                   Thread.sleep(1);
//               } catch (InterruptedException e) {
//                   throw new RuntimeException(e);
//               }  // 修改逻辑, 就可以避免出现内存可见性导致的死锁
           }
            System.out.println("t1 over");
        });
        Thread t2= new Thread(()->{
            System.out.println("please enter the value of flag");
            Scanner scanner = new Scanner(System.in);
            flag = scanner.nextInt();
        });

        t1.start();
        t2.start();

    }
}
